home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP09.ZIP / CHAP09 / PATRON / PAGEMOUS.CPP < prev    next >
C/C++ Source or Header  |  1993-06-13  |  19KB  |  761 lines

  1. /*
  2.  * PAGEMOUS.CPP
  3.  * Modifications for Chapter 9
  4.  *
  5.  * Implementation of mouse-related member functions of CPage.
  6.  * The remainder is in PAGE.CPP.  This separate file keeps this
  7.  * grungy hit-testing/drawing code out of our way.
  8.  *
  9.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19. #include "patron.h"
  20.  
  21.  
  22. //Lookups into the array using g_rgHTCode[x+y*3] in PAGEMOUS.CPP
  23. #define YTOP            0
  24. #define YMID            1
  25. #define YBOT            2
  26. #define XLEFT           0
  27. #define XMID            1
  28. #define XRIGHT          2
  29.  
  30. //Values to restrict sizing in CPage::OnMouseMove
  31. #define SIZINGTOP       0x0001
  32. #define SIZINGBOTTOM    0x0002
  33. #define SIZINGLEFT      0x0004
  34. #define SIZINGRIGHT     0x0008
  35.  
  36.  
  37. //This array is for hit-testing lookups
  38. static UINT g_rgHTCode[9]={HTTOPLEFT, HTTOP, HTTOPRIGHT
  39.     , HTLEFT, HTCLIENT, HTRIGHT, HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT};
  40.  
  41.  
  42. //This is for restricting tracking based on the hit-test
  43. static UINT g_rguSizingFlags[9]={SIZINGTOP | SIZINGLEFT, SIZINGTOP
  44.     , SIZINGTOP | SIZINGRIGHT, SIZINGLEFT, 0, SIZINGRIGHT
  45.     , SIZINGBOTTOM | SIZINGLEFT, SIZINGBOTTOM, SIZINGBOTTOM | SIZINGRIGHT};
  46.  
  47.  
  48. //CHAPTER9MOD
  49. /*
  50.  * CPage::OnRightDown
  51.  *
  52.  * Purpose:
  53.  *  Called when the user clicks with the right button on this page.
  54.  *  If there is an object here, determined by the last hit-test code,
  55.  *  the we'll make a popup-menu for it.
  56.  *
  57.  * Parameters:
  58.  *  uKeys           UINT carrying the key state.
  59.  *  x, y            UINT coordinates of the click in device units.
  60.  *
  61.  * Return Value:
  62.  *  BOOL            Indicates if the action changed the object.
  63.  */
  64.  
  65. BOOL CPage::OnRightDown(UINT uKeys, UINT x, UINT y)
  66.     {
  67.     HMENU       hMenu;
  68.     HMENU       hMenuRes;
  69.     HINSTANCE   hInst;
  70.     HWND        hWndFrame, hWndT;
  71.     POINT       pt;
  72.     UINT        i, cItems;
  73.  
  74.     //Select the tenant under the mouse, if there is one.
  75.     if (!FSelectTenantAtPoint(x, y))
  76.         return FALSE;
  77.  
  78.     /*
  79.      * Get the top-level window to which menu command will go.  This will
  80.      * be whatever parent doesn't have a parent itself...
  81.      */
  82.     hWndT=GetParent(m_hWnd);
  83.  
  84.     while (NULL!=hWndT)
  85.         {
  86.         hWndFrame=hWndT;
  87.         hWndT=GetParent(hWndT);
  88.         }
  89.  
  90.     /*
  91.      * Build a popup menu for this object with Cut, Copy, Delete, and
  92.      * object verbs.
  93.      */
  94.     hInst=GETWINDOWINSTANCE(m_hWnd);    //Macro in WIN1632.H
  95.     hMenuRes=LoadMenu(hInst, MAKEINTRESOURCE(IDR_RIGHTPOPUPMENU));
  96.  
  97.     if (NULL==hMenuRes)
  98.         return FALSE;
  99.  
  100.     //Resource-loaded menus don't work, so we'll copy the items.
  101.     hMenu=CreatePopupMenu();
  102.     cItems=GetMenuItemCount(hMenuRes);
  103.  
  104.     for (i=0; i < cItems; i++)
  105.         {
  106.         char        szTemp[80];
  107.         int         id, uFlags;
  108.  
  109.         GetMenuString(hMenuRes, i, szTemp, sizeof(szTemp), MF_BYPOSITION);
  110.         id=GetMenuItemID(hMenuRes, i);
  111.  
  112.         uFlags=(0==id) ? MF_SEPARATOR : MF_STRING | MF_ENABLED;
  113.         AppendMenu(hMenu, uFlags, id, szTemp);
  114.         }
  115.  
  116.     DestroyMenu(hMenuRes);
  117.  
  118.     //Munge the Object menu item
  119.     m_pTenantCur->AddVerbMenu(hMenu, MENUPOS_OBJECTONPOPUP);
  120.  
  121.     SETPOINT(pt, x, y);
  122.     ClientToScreen(m_hWnd, &pt);
  123.  
  124.     TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON
  125.         , pt.x, pt.y, 0, hWndFrame, NULL);
  126.  
  127.     DestroyMenu(hMenu);
  128.     return FALSE;
  129.     }
  130.  
  131.  
  132.  
  133.  
  134. /*
  135.  * CPage::FSelectTenantAtPoint
  136.  *
  137.  * Purpose:
  138.  *  Selects whatever tenant is at the point (x,y) if there is one,
  139.  *  deselecting the previously selected tenant.
  140.  *
  141.  * Parameters:
  142.  *  x, y            UINT coordinates of the mouse.
  143.  *
  144.  * Return Value:
  145.  *  BOOL            TRUE if there is a tenant here, FALSE otherwise.
  146.  */
  147.  
  148. BOOL CPage::FSelectTenantAtPoint(UINT x, UINT y)
  149.     {
  150.     UINT            iTenant;
  151.     LPTENANT        pTenant;
  152.  
  153.     iTenant=TenantFromPoint(x, y, &pTenant);
  154.  
  155.     if (NULL==pTenant)
  156.         return FALSE;
  157.  
  158.     //If this one is already current, we might be now sizing.
  159.     if (pTenant==m_pTenantCur)
  160.         return TRUE;
  161.  
  162.     //Deselect the current tenant
  163.     if (NULL!=m_pTenantCur)
  164.         m_pTenantCur->Select(FALSE);
  165.  
  166.     //Move this tenant to the top of the list
  167.     m_iTenantCur=0;
  168.  
  169.     SendMessage(m_hWndTenantList, LB_DELETESTRING, iTenant, 0L);
  170.     SendMessage(m_hWndTenantList, LB_INSERTSTRING, 0, (LONG)pTenant);
  171.  
  172.     //Select and repaint the new tenant to show it up front
  173.     m_pTenantCur=pTenant;
  174.  
  175.     m_pTenantCur->Repaint();
  176.     m_pTenantCur->Select(TRUE);
  177.  
  178.     return TRUE;
  179.     }
  180.  
  181. //End CHAPTER9MOD
  182.  
  183.  
  184.  
  185. /*
  186.  * CPage::OnLeftDown
  187.  *
  188.  * Purpose:
  189.  *  Called when the user clicks with the left button on this page.
  190.  *  We find the object under that position that is visibly on top
  191.  *  (always the first one under this location in the page list since
  192.  *  we paint in reverse order) and select it.
  193.  *
  194.  * Parameters:
  195.  *  uKeys           UINT carrying the key state.
  196.  *  x, y            UINT coordinates of the click in device units.
  197.  *
  198.  * Return Value:
  199.  *  BOOL            Indicates if the action changed the object.
  200.  */
  201.  
  202. BOOL CPage::OnLeftDown(UINT uKeys, UINT x, UINT y)
  203.     {
  204.     RECT        rc;
  205.  
  206.     if (HTCAPTION==m_uHTCode)
  207.         return DragDrop(uKeys, x, y);
  208.  
  209.     /*
  210.      * See if we have to start sizing (which always happens on current
  211.      * tenant).  m_uHTCode is set in OnNCHitTest below.
  212.      */
  213.     if (HTNOWHERE!=m_uHTCode && HTCLIENT!=m_uHTCode)
  214.         {
  215.         //We are sizing, so start tracking
  216.         m_pTenantCur->RectGet(&m_rcl, TRUE);
  217.         SetCapture(m_hWnd);
  218.         m_fTracking=TRUE;
  219.  
  220.         m_hDC=GetDC(m_hWnd);
  221.  
  222.         //Place the rectangle exactly where it is on the screen.
  223.         RECTFROMRECTL(rc, m_rcl)
  224.         OffsetRect(&rc, -(int)m_pPG->m_xPos, -(int)m_pPG->m_yPos);
  225.         RECTLFROMRECT(m_rcl, rc);
  226.         m_rclOrg=m_rcl;
  227.  
  228.         DrawFocusRect(m_hDC, &rc);
  229.  
  230.         m_pPG->CalcBoundingRect(&rc, TRUE);
  231.         RECTLFROMRECT(m_rclBounds, rc);
  232.         return FALSE;
  233.         }
  234.  
  235.     //CHAPTER9MOD
  236.     //Selection logic moved to a function so OnRightDown can use it.
  237.     FSelectTenantAtPoint(x, y);
  238.     //End CHAPTER9MOD
  239.  
  240.     return FALSE;
  241.     }
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. /*
  250.  * CPage::OnLeftUp
  251.  *
  252.  * Purpose:
  253.  *  Called when the user clicks up with the left button on this page.
  254.  *  We stop tracking on this message, if necessary, and resize the object.
  255.  *
  256.  * Parameters:
  257.  *  uKeys           UINT carrying the key state.
  258.  *  x, y            UINT coordinates of the click in device units.
  259.  *
  260.  * Return Value:
  261.  *  BOOL            Indicates if this action changed the object.
  262.  */
  263.  
  264. BOOL CPage::OnLeftUp(UINT uKeys, UINT x, UINT y)
  265.     {
  266.     RECT    rc, rcT;
  267.  
  268.     if (!m_fTracking)
  269.         return FALSE;
  270.  
  271.     //Remove the dotted rectangle.
  272.     RECTFROMRECTL(rc, m_rcl)
  273.     DrawFocusRect(m_hDC, &rc);
  274.     ReleaseDC(m_hWnd, m_hDC);
  275.  
  276.     ReleaseCapture();
  277.     m_fTracking=FALSE;
  278.  
  279.     //If the original and new rects are the same, nothing happened.
  280.     RECTFROMRECTL(rcT, m_rclOrg);
  281.  
  282.     if (EqualRect(&rc, &rcT))
  283.         return FALSE;
  284.  
  285.     RECTFROMRECTL(rcT, m_rclOrg);
  286.     InvalidateRect(m_hWnd, &rcT, TRUE);
  287.  
  288.     //Invalidate on the screen before accounting for scrolling
  289.     InvalidateRect(m_hWnd, &rc, TRUE);
  290.  
  291.     //Factor in the scrolling and tell the tenant where it now stands.
  292.     OffsetRect(&rc, (int)m_pPG->m_xPos, (int)m_pPG->m_yPos);
  293.     RECTLFROMRECT(m_rcl, rc);
  294.     m_pTenantCur->RectSet(&m_rcl, TRUE);
  295.  
  296.     UpdateWindow(m_hWnd);
  297.     return TRUE;
  298.     }
  299.  
  300.  
  301.  
  302.  
  303.  
  304. /*
  305.  * CPage::OnLeftDoubleClick
  306.  *
  307.  * Purpose:
  308.  *  Called when the user double-clicks with the left button on this page.
  309.  *  We find the object under that position that is visibly on top
  310.  *  (always the first one under this location in the page list since
  311.  *  we paint in reverse order) and activate it.
  312.  *
  313.  * Parameters:
  314.  *  uKeys           UINT carrying the key sta